#include "edituser.h"
#include "ui_edituser.h"
#include "string"
#include <QCryptographicHash>
#include "QMessageBox"
#include "QDebug"
#include "../MainProject/sqlitedatabase.h"
#include "../MainProject/loggermanager.h"
EditUser::EditUser(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::EditUser),firstwindow(nullptr)
{
    ui->setupUi(this);
       window()->setWindowTitle("修改用户");


}

void EditUser::setFirstWindow(AdminManager *firstWindow)
{
    this->firstwindow=firstWindow;

}

QString EditUser::hashPassword(const QString &password)
{
    // 创建一个 QCryptographicHash 对象，设置其使用 MD5 算法
    QCryptographicHash hasher(QCryptographicHash::Md5);

    // 添加密码数据到哈希对象中（注意：转为 QByteArray）
    hasher.addData(password.toUtf8());

    // 计算哈希值并获取结果，转换为十六进制格式字符串
    QString hashedPassword = hasher.result().toHex();

    return hashedPassword;
}

bool EditUser::validateEmail(const QString &email)
{
    QRegularExpression regex(R"((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$))");
    return regex.match(email).hasMatch();
}

bool EditUser::validatePhoneNumber(const QString &phoneNumber)
{
    QRegularExpression regex(R"(^1[3-9]\d{9}$)");
    return regex.match(phoneNumber).hasMatch();
}

void EditUser::setUiEdit(const QString &name, const QString &phone, const QString &emil)
{   qDebug()<<"ename"<<name;
    ui->userNameEdit->setText(name);
    ui->EmailEdit->setText(emil);
    ui->phoneEdit->setText(phone);


}





EditUser::~EditUser()
{
    delete ui;
}

void EditUser::on_saveButton_clicked()
{

    QString password;
    QString name= ui->userNameEdit->text();
    QString passwords=ui->passwordEdit->text();
    password=hashPassword(passwords);
    QString email=ui->EmailEdit->text();
    QString phone=ui->phoneEdit->text();
    bool phoneflag=true;
    bool emailflag=true;
    bool phonege=true;
    bool emailge=true;
    if(!phone.isEmpty()){

        if(!validatePhoneNumber(phone)){
             QMessageBox::information(this,"waring","手机号码格式不正确");
          //  Logger().logError("手机号码格式不正确");
            phonege=false;
        }/*else{
        //查询手机号码
        SQLiteDatabase* db = SQLiteDatabase::getInstance();
        QString condition = QString("phone = '%1'").arg(phone); // 设置条件字符串
        QJsonArray result = db->read("users", condition); // 调用read方法获取结果
        // for (const QJsonValue& value : result) {
        //     QJsonObject obj = value.toObject();
        //     qDebug() << "User:" << obj;
        // }
        if(!result.isEmpty()){

            QMessageBox::information(this, "waring","你要修改的手机号已存在");
         //   Logger().logError("修改的手机号码已存在");
            phoneflag=false;
        }
        }*/
    }

    if(!email.isEmpty()){
        if(!validateEmail(email)){
            QMessageBox::information(this,"waring","邮箱格式不正确");
          //  Logger().logError("邮箱格式不正确");
            emailge=false;
        }
        // else{
        // //查询邮箱
        // SQLiteDatabase* db = SQLiteDatabase::getInstance();
        // QString condition = QString("email = '%1'").arg(email); // 设置条件字符串
        // QJsonArray result = db->read("users", condition); // 调用read方法获取结果
        // // for (const QJsonValue& value : result) {
        // //     QJsonObject obj = value.toObject();
        // //     qDebug() << "User:" << obj;
        // // }
        // // if(!result.isEmpty()){

        // //     QMessageBox::information(this, "waring","你要修改的邮箱已存在");
        // //    // Logger().logError("修改的邮箱已存在");
        // //     emailflag=false;
        // // }
     //   }

    }

    if(name.isEmpty()){

         QMessageBox::information(this, "waring", "请输入你要修改的用户");
    }



    if(!name.isEmpty()){
        //查询用户名
        SQLiteDatabase* db = SQLiteDatabase::getInstance();
        QString condition = QString("username = '%1'").arg(name); // 设置条件字符串
        QJsonArray result = db->read("users", condition); // 调用read方法获取结果
        // for (const QJsonValue& value : result) {
        //     QJsonObject obj = value.toObject();
        //     qDebug() << "User:" << obj;
        // }
        if(result.isEmpty()){
          //  qDebug() << "找不到你要修改的用户" ;
              QMessageBox::information(this, "waring", "找不到你要修改的用户");

         // Logger().logError("找不到要修改的用户");
        }else{


            if(passwords.isEmpty()){
                QMessageBox::information(this, "waring", "修改密码不能为空");
               //   Logger().logError("修改的密码为空");
            }
            if(email.isEmpty()){
                QMessageBox::information(this, "waring", "修改邮箱不能为空");
               //   Logger().logError("输入修改的邮箱为空");
            }
            if(phone.isEmpty()){
                QMessageBox::information(this, "waring", "修改手机号不能为空");
              //   Logger().logError("输入修改的手机号码为空");
            }
            if(!passwords.isEmpty()&&!email.isEmpty()&&!phone.isEmpty()&&phoneflag && emailflag && phonege && emailge){
                 QString condition = QString("username = '%1'").arg(name); // 设置条件字符串
                QJsonObject data;
                data["password"] = password;
                data["email"] = email;
                data["phone"] = phone;
                //执行修改操作
                db->update("users", data, condition);
                QMessageBox::information(this, "waring", "用户修改成功");
                // Logger().logInfo("输入修改的手机号码为空");

                this->hide();
                QJsonArray result2 = db->read("users", "");


                firstwindow->selectAllUsers(result2);

            }

        }


    }




}

